# 输入年龄页面
# 值：01检查号码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QLineEdit, qApp, QDesktopWidget, QLabel, QPushButton, QMessageBox,QTextEdit
from PyQt5.QtGui import QIcon, QFont,QKeyEvent
from PyQt5.QtCore import Qt


class Result(QMainWindow):
    def __init__(self, jianchahao_value='222'):
        super().__init__()
        # 传递检查号码
        global jianchahao_text, jifen_text
        jianchahao_text = jianchahao_value  # 传递来自检查号的页面数据

        self.initUI()

    def initUI(self):

        import sqlite3
        conn = sqlite3.connect('test.db')
        #print ("Opened database successfully")
        str_chaxun = "select * from COMPANY where NAME ='{0}'".format(jianchahao_text)
        curse = conn.execute(str_chaxun)
        sum_all = 0
        globals()["zhou_jifen"]=0
        globals()["yue_jifen"]=0
        globals()["sanjiao_jifen"]=0
        globals()["daduojiao_jifen"]=0
        globals()["xiaoduojiao_jifen"]=0
        globals()["tou_jifen"]=0
        globals()["gou_jifen"]=0
        globals()["gender"]=0
        for row in curse:
            gender = row[4]
            zhou_jifen = row[5]
            yue_jifen =row[6]
            sanjiao_jifen = row[7]
            daduojiao_jifen = row[8]
            xiaoduojiao_jifen = row[9]
            tou_jifen = row[10]
            gou_jifen = row[11]
            sum_all = row[5]+row[6]+row[7]+row[8]+row[9]+row[10]+row[11]
       # print(sum_all,zhou_jifen,yue_jifen,sanjiao_jifen,daduojiao_jifen,xiaoduojiao_jifen,tou_jifen,gou_jifen)
        conn.close()
        ####报告区####
            ##钩骨

        ####结论区####
        if (int(sum_all) < 1000):
            topAge = self.getAge(gender,sum_all,'+')
            #bottomAge = self.getAge(gender,sum_all,'-')
        else:
            if gender == 0:
                #gender_str = '女'
                topAge = '13'
                bottomAge = '13'
            else:
                #gender_str = '男'
                topAge = '15'
                bottomAge = '15'

        if gender == 0:
            gender_str = '女'
        else:
            gender_str = '男'
        #content_report = 'XXX'
        result_report = '根据TW3-C Carpal骨龄法所示，左手骨龄相当于{0}周岁{1}性儿童骨龄。'.format(topAge,gender_str)
        lbl_jianchahao = QLabel('结论', self)
        lbl_jianchahao.setFont(QFont('SansSerif', 20))
        lbl_jianchahao.resize(200, 50)
        lbl_jianchahao.move(225, 0)

        lbl_jiegou = QLabel('舟 状 骨:'+str(zhou_jifen)+
                            '   月    骨:'+str(yue_jifen)+
                            '   三 角 骨:'+str(sanjiao_jifen)+
                            '   大多角骨:'+str(daduojiao_jifen)+
                            ' \n小多角骨:'+str(xiaoduojiao_jifen)+
                            '    头 状 骨:'+str(tou_jifen)+
                            '  钩 状 骨:'+str(gou_jifen), self)
        lbl_jiegou.setFont(QFont('SansSerif', 10))
        lbl_jiegou.resize(500, 150)
        lbl_jiegou.move(20, 0)
        #描述显示区域
        # content = QTextEdit(self)
        # content.setPlainText(content_report)
        # content.resize(400,200)
        # content.move(30,120)

        #结论显示区域
        result = QTextEdit(self)
        result.setPlainText(result_report)
        result.resize(450,100)
        result.move(30,120)

        btn = QPushButton('再测一个', self)
        btn.setFont(QFont('SansSerif', 14))
        btn.resize(100, 60)
        btn.move(200, 300)
        btn.clicked.connect(self.on_back_clicked)

        self.setFixedSize(500, 400)
        self.setWindowTitle('玉环县人民医院 TW3骨龄法 软件')
        self.setWindowIcon(QIcon("images/myicon.png"))
        self.center()
        self.show()

    # 点击返回按钮的时候 引入主界面并将自己销毁
    def on_back_clicked(self):
        from jianchahao import Example
        self.window().hide()
        self.diag = Example()
        self.diag.show()


    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def keyPressEvent(self, event):
        keyEvent = QKeyEvent(event)
        if (keyEvent.key() == Qt.Key_Enter) or (keyEvent.key() == Qt.Key_Return):
            self.on_next_clicked()
    def closeEvent(self, QCloseEvent):
        reply = QMessageBox.question(self, '退出？', "确定退出吗?",
                                     QMessageBox.Yes, QMessageBox.No)
        if reply == QMessageBox.Yes:
            QCloseEvent.accept()
            qApp.quit
        else:
            QCloseEvent.ignore()

    ##获取数据库里的骨龄信息，返回骨龄数字
    def getAge(self,gender,sum_score,fuhao):
        import sqlite3
        conn = sqlite3.connect('test.db')

        if gender == 0:
             str_chaxun_biao = "select * from FEMALE where SCORE ={0}".format(sum_score)
        else:
             str_chaxun_biao = "select * from MALE where SCORE ={0}".format(sum_score)

        #str_chaxun_biao_male = "select * from MALE where SCORE ={0}".format(sum_score)
        curse01 = conn.execute(str_chaxun_biao)

        if curse01.fetchall():
            for data in curse01:
                #print(data[0])
                return (data[1])
        else:
             for i in range(1,30):
                #print(sum_all)
                if fuhao == '+':
                    sum_score += 1
                else:
                    sum_score -= 1

                if gender == 0:
                    str_chaxun_biao_male_02 = "select * from FEMALE where SCORE ={0}".format(sum_score)
                else:
                    str_chaxun_biao_male_02 = "select * from MALE where SCORE ={0}".format(sum_score)

                curse02 = conn.execute(str_chaxun_biao_male_02)
                if curse02.fetchall():
                    #curse02 = conn.execute(str_chaxun_biao_male_02)
                    for child in curse02:
                        return (child[1])
                    break
                else:
                    pass
        conn.close()
